<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/6/6
 * Time: 17:38
 */

namespace app\common\model;

use app\common\lib\exception\ApiException;
use think\Cache;
use XznsTools\cache\CacheManage;
use app\common\cachemodel\IndexButton;

class Button extends Base
{
    protected $table = 'wechatadmin.system_index_button';

    protected $appType = [
        'android' => 1,
        'ios' => 2
    ];

    /**
     * 获取按钮
     * @param array $map
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws ApiException
     * @throws \Exception
     */
    public function getButton($map = [])
    {
        $ver = $map['ver'];

        $data = [];

        if ($this->cache_on > 0)
        {
            $index_button = Cache::get('index_button_cat_' . $ver);
            $data = Cache::get('index_btns_' . $ver);
        }
        if (empty($index_button))
        {
            try
            {
                $index_button = $this->table('wechatadmin.system_index_button_cat')
                    ->field('id,bid')
                    ->where(['status' => 1, 'ver' => $ver])->findOrFail()->toArray();

                Cache::set('index_button_cat_' . $ver, $index_button, 86400 * 15);


            }
            catch (\Exception $e)
            {
                $index_button = [];
            }
        }
        if (empty($data) && !empty($index_button))
        {
            try
            {

                $bids = unserialize($index_button['bid']);

                $field = 'name,icon,type,is_new';

                $data = $this->field($field)->where(['id' => ['in', $bids]])->order('sort DESC')->selectOrFail();

                $data = json_decode(json_encode($data, true), true);

                Cache::set('index_btns_' . $ver, $data, 86400 * 15);

            }
            catch (\Exception $e)
            {
//                \think\Log::write('首页按钮获取失败：' . $e->getMessage(), 'index_slide_error', true);
//                throw new ApiException('首页按钮获取失败');
                $data = [];
            }
        }
        if ($data === false) $data = [];

        return $data;
    }


    /**
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     * @throws \Exception
     */
    public function getButtons($map = [])
    {
        $ver = $map['ver'];
        $data = [];
        $app_type = in_array($ver, ['1.1.1', '1.3.0', '1.1.1', '1.2.1', '1.2.0', '1.2.1', '1.3.0', '1.3.1', '1.3.2', '1.4.0', '1.4.1', '1.4.2', '1.4.3', '1.5.0', '1.5.1', '1.5.2', '1.6.0', '1.6.1', '1.6.2', '1.7.0', '1.7.1', '1.7.2', '1.8.0', '1.8.1', '1.9.0', '1.9.1', '1.9.2']) ? 0 : ($this->appType[$map['app_type']] ?? 0);

        $index_button = false;
        if ($this->cache_on > 0)
        {
//            $index_button = Cache::get('index_button_cat_' . $ver . '_' . $app_type);
//            $data = Cache::get('index_btns_' . $ver . '_' . $app_type);

            $index_button = (new IndexButton())->getIndexButton(['ver'=>$ver,'app_type'=>$app_type]);
            $data = CacheManage::getInstance()->setCacheConfig('index.index_btns')->getCache(['name,icon,type,is_new']);
        }

        if (!$index_button)
        {
            try
            {
                $index_button = $this->table('wechatadmin.system_index_button_cat')
                    ->field('id,bid')
                    ->where(['status' => 1, 'ver' => $ver, 'app_type' => $app_type])->findOrFail()->toArray();

//                Cache::set('index_button_cat_' . $ver . '_' . $app_type, $index_button, 86400 * 15);

                CacheManage::getInstance()->setCacheConfig('index.index_button_cat', $ver . ':' . $app_type)->setCache($index_button, 'details');
            }
            catch (\Exception $e)
            {
                $index_button = [];
            }
        }

        if (empty($data) && $index_button)
        {
            try
            {

                $bids = unserialize($index_button['bid']);

                $field = 'name,icon,type,is_new';

                $data = $this->field($field)->where(['id' => ['in', $bids]])->order('sort DESC')->selectOrFail();

                $data = json_decode(json_encode($data), true);

//                Cache::set('index_btns_' . $ver . '_' . $app_type, $data, 86400 * 15);

                CacheManage::getInstance()->setCacheConfig('index.index_btns', $ver . ':' . $app_type)->setCache($data, 'details');

            }
            catch (\Exception $e)
            {
//                \think\Log::write('首页按 钮获取失败：' . $e->getMessage(), 'index_slide_error', true);
//                throw new ApiException('首页按钮获取失败');
                $data = [];
            }
        }
        if ($data === false) $data = [];

        return $data;

    }
}
